package com.tempmonitingapp;

import com.facebook.react.ReactActivity;
import com.tempmonitingapp.android_upgrade.DownloadService;

import cn.jpush.android.api.JPushInterface;

import android.annotation.TargetApi;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.provider.SyncStateContract;
import android.util.Log;
import android.widget.Toast;

public class MainActivity extends ReactActivity {

    /**
     * Returns the name of the main component registered from JavaScript.
     * This is used to schedule rendering of the component.
     */

    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
                if(intent != null) {
                        String downloadUri = intent.getStringExtra("downloadUri");
                        aa(downloadUri);
                }
        }
    };
    @Override
    protected String getMainComponentName() {
        return "tempMonitingApp";
    }
    @TargetApi(Build.VERSION_CODES.O)
    public void aa(String downloadUri) {
        Uri packageUri = Uri.parse("package:" + getPackageName());
        Intent intent1 = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageUri);
        Bundle v = new Bundle();
        v.putString("KAKA",downloadUri);
        startActivityForResult(intent1,100);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        JPushInterface.init(this);
        // Log.i("MainActivity", "onCreate executed!");
        IntentFilter filter = new IntentFilter("downloadPermission");
        registerReceiver(receiver,filter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 100){
//            String downuri = data.getStringExtra("downuri");
            Intent intent = new Intent(this, DownloadService.class);
            intent.putExtra("url","http://192.168.101.142:8189/monitor/app-debug(1).apk");
            startService(intent);
        }
    }
}
